// pages/coupon/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CouponList:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getCoupon(e){
    let coupons = wx.getStorageSync('coupons')||[]//获取内存中的优惠券数据
    coupons.unshift(e.currentTarget.dataset.coupon)//添加新的优惠券到数据中
    wx.showToast({//显示提示
      title: '领取成功',
    })
    wx.setStorageSync('coupons', coupons)
    let value = {
      coupons:coupons
    }
    app.Update("users","_openid",app.globalData.openid,value)//更新已用户领取的优惠券
    var that =this
    setTimeout(v=>{ that.judgecoupon()},1500)//1.5秒后刷新可领取的优惠券
  },
  judgecoupon(){
    wx.showLoading({
      title: "加载中",
      mask: true
  });//加载数据库中可以供用户领取的优惠券
    let CouponList = app.globalData.couponList;
    let coupons = wx.getStorageSync('coupons')||[]
   coupons.forEach(v2=>{
    CouponList = CouponList.filter(v1=>{
        return v1.cid!=v2.cid
      })
    })
    this.setData({
      CouponList:CouponList
    })
    wx.hideLoading();
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.judgecoupon()
  },

})